package com.qf.application.controller;

import com.qf.application.commons.annotation.SysLog;
import com.qf.application.commons.utils.R;
import com.qf.application.entity.Menu;
import com.qf.application.entity.User;
import com.qf.application.entity.UserRoleVo;
import com.qf.application.service.IAllotService;
import com.qf.application.vo.RoleMenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/allot")
@Slf4j
public class AllotController {

    @Autowired
    private IAllotService allotService;

    //    @SysLog(value = "给用户授权")
//    @SysLog(value = "给用户授权",name = "xxx")
    @SysLog("给用户授权") // 调用加了@SysLog注解的方法需要额外的记录用户的操作日志
    @GetMapping("/test")
    public R test(@RequestBody Map<String,Object> map) {
        log.debug("处理给用户授权的业务");
        return R.ok();
    }


    // 查询某个角色拥有过的菜单
    @GetMapping("/getMenuIdListByRoleId/{roleId}")
    public R getMenuIdListByRoleId(@PathVariable Integer roleId) {

        // 1.记录用户操作日志(系统服务) 系统服务和业务逻辑完全耦合了

        List<Integer> menuList = allotService.getMenuIdListByRoleId(roleId);
        return R.ok().put("data", menuList);
    }

    // 给角色添加菜单的权限
    @PostMapping("/roleAddMenu") // vo(ViewObject/entity)
    public R roleAddMenu(@RequestBody RoleMenuVo roleMenuVo) {

        // 1.记录用户操作日志

        return R.ok(allotService.roleAddMenu(roleMenuVo));
    }

    @GetMapping("/getUserByRoleId/{roleId}")
    public R getUserByRoleId(@PathVariable Integer roleId) {
        return R.ok().put("data", allotService.getUserByRoleId(roleId));
    }

    @PostMapping("/userAddRole")
    public R userAddRole(@RequestBody UserRoleVo roleVo) {
        return R.write(allotService.userAddRole(roleVo));
    }

    @PostMapping("/removeUserRole")
    public R removeUserRole(@RequestBody UserRoleVo roleVo) {
        allotService.removeUserRole(roleVo.getRoleId(), roleVo.getUserIdList().get(0));
        return R.ok();
    }
}
